package moe.bogos.douro.api.common.capabilities;

import moe.bogos.douro.api.DouroAPI;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.INBT;
import net.minecraftforge.common.capabilities.CapabilityManager;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;

/**
 * 能帮助你更方便使用capabilities的类
 * 你只需要用set和get两个方法便可以操作玩家NBT
 *
 * @author hookan
 */
@Mod.EventBusSubscriber(modid = DouroAPI.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
public class CapHelper
{
    /**
     * 设置玩家附加NBT
     *
     * @param player 玩家对象
     * @param key    键
     * @param value  值
     */
    public static void set(PlayerEntity player, String key, INBT value)
    {
        ICapMap map = CapMapProvider.get(player);
        map.getMap().put(key, value);
        map.dataChanged(player);
    }

    /**
     * 获取玩家的附加NBT（必须由本类设置的附加NBT才能获取）
     *
     * @param player 玩家对象
     * @param key    键
     */
    public static INBT get(PlayerEntity player, String key)
    {
        ICapMap map = CapMapProvider.get(player);
        return map.getMap().get(key);
    }
    
    @SubscribeEvent
    public static void init(final FMLCommonSetupEvent event)
    {
        CapabilityManager.INSTANCE.register(ICapMap.class, new CapMapStorage(), CapMap::new);
    }
}
